
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<span style="font-size:18px;">
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="purchase/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="purchase/css/commonly-used-show.css" />
    <script type="text/javascript" src="purchase/js/jquery.min.js"></script>
    <script type="text/javascript" src="purchase/js/bootstrap.js" ></script>
    <!--  缓存 -->
    <script type="text/javascript" src="purchase/js/jquery.cookie.js"></script>

    <!--  提示框 -->
    <link rel="stylesheet" type="text/css" href="purchase/css/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="purchase/css/modal.css" />
    <script type="text/javascript" src="purchase/js/modal.js" ></script>

    <!--  分页显示 -->
    <script src="purchase/js/jqPaginator.min.js" type="text/javascript"></script>
    <link href="purchase/css/myPage.css" rel="stylesheet" type="text/css" />
    <script src="purchase/js/myPage.js" type="text/javascript"></script>


    <title>出库查询</title>

    <style type="text/css">
        td, th {
            text-align: center;
        }
    </style>

</head>

<body class="mybody">

 <jsp:include page="theTopMenuAdmin.jsp" />

 <div class="container-fluid">
    <div class="row">


        <div class="col-md-12">
            <div class="col-md-12">
                    <div class="panel panel-primary">
                        <div class="panel-body">


                            <div style="margin-left: 50px; margin-right: 50px;">

                                 <h3 style="text-align: center">出库信息列表</h3>

    <table border="1" class="table table-bordered table-hover">
        <tr class="success">
            <th>序号</th>
            <th>商品编号</th>
            <th>商品类别</th>
            <th>数量</th>
            <th>出库时间</th>
            <th>供货商</th>
            <th>操作</th>
        </tr>

        <tbody id="tbody-result">
        </tbody>

    </table>
                            </div>



                            <div class="text-center">


                                <div style="margin-top: 20px;">

                                    <div style="float: left; margin-left: 130px;" class="col-lg-4 ">
                <div class="input-group">
                    <input id="user_find" type="text" class="form-control" placeholder="类别或供货商或库存时间查找">
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="button" onclick="findUses()">查找</button>
                    </span>
                </div>

            </div>


                                    <div style="float: right; margin-right: 100px;">
                                        <a class="btn btn-primary" href="javascript:void(0);" onclick="queryOutbunInfo1()">导出</a>
                                    </div>

                                    <div style="float: right; margin-right: 50px;">
                                        <a class="btn btn-primary" href="${pageContext.request.contextPath}/addDelivery?id=1">添加出库</a>
                                    </div>



                                    <div style="clear: both">

                                    </div>

                                </div>

                                <div style="margin-top: 20px;">
                                    <span id="user_messge" style="font-size:10px; color: red;"></span>
                                </div>

                                <div>
                                    <ul class="pagination" id="pagination"></ul>
                                    <!--PageCount是一共多少条数据-->
                                    <input type="hidden" id="PageCount" />
                                    <!--PageSize是页面显示多少条数据-->
                                    <input type="hidden" id="PageSize" value="6"/>
                                    <!--countindex这个参数是通过上面的PageCount与PageSize计算出来的分多少页-->
                                    <!--js中是自动计算的，可默认也可以自己填-->
                                    <input type="hidden" id="countindex" value="10"/>
                                    <!--上一页与下一页中间的数字框，要显示几个就value多少个-->
                                    <input type="hidden" id="visiblePages" value="7"/>
                                </div>

                            </div>



                        </div>
                    </div>
                </div>
        </div>



    </div>

 </div>


<script type="text/javascript">
    var userdt = JSON.parse(sessionStorage.getItem("user"));
    if (userdt.result.userName != null){
        $("#usname").text(userdt.result.userName);
    }else {
        $("#usname").text(userdt.result.userLoginName);
    }

    //设置全局变量
    var numes = 1;
    var isok = true;
    var count;

    /*loadData(num,count)写在$(function(){})外，num为当前选中页码，count是我自己加的，是ajax返回的数据总数*/
    function loadData(num) {
        numes = num;
        if (count){
            $("#PageCount").val(count);
        }else {
            $("#PageCount").val("5");
        }

        //当点击首页，下一页，上一页，尾页，数字页码时执行ajax数据操作
        if (numes !== 1){
            queryOutbunInfo();
        }else if (numes === 1){
            queryOutbunInfo();
        }

    }

    //输入查找的条件时进行查找操作
    function findUses(){
        isok = true;
        queryOutbunInfo();
    }

    //每当数据发生变动时进行页面的刷新
    function uppag(){
        //重新加载分页控件
        loadData(1);
        loadpage();
        isok = false;
    }

    window.onload = function(){
        queryOutbunInfo();
    }

    //进行ajax异步请求，完成数据的查询
    function queryOutbunInfo(){
        var postUrl = "${pageContext.request.contextPath}/member/allInboundOutboundList";
        var str = {
            "pageResult": {
                "currentPage": numes,
                "pageSize": 6
            },
            "userType": "2",
            "productType": $("#user_find").val(),
            "deliveryTime": $("#user_find").val(),
            "supplier": $("#user_find").val()
        };
        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status === 0){
                    $("#user_messge").text(data.msg);

                    if (data.msg === "查询失败,无该数据"){
                        count = 0;
                        uppag();
                        $("#tbody-result").html("");
                    }

                }else {
                    //全局变量，第一次加载后就不执行了，当用户进行查找操作时在继续执行
                    if (isok){
                        count = data.result.totalSize;
                        uppag();
                    }

                    var strtab = "";
                    //清空表格数据
                    $("#tbody-result").html("");
                    //自定义序号
                    var conter = 1;

                    for (var i = 0; i < data.result.items.length; i++) {

                        //对数据进行拼接，在显示出来
                        strtab = "<tr>" + "<td>" + conter + "</td>"
                            + "<td>" + data.result.items[i].productId + "</td>"
                            + "<td>" + data.result.items[i].productType + "</td>"
                            + "<td>" + data.result.items[i].quantity + "</td>"
                            + "<td>" + data.result.items[i].deliveryTime + "</td>"
                            + "<td>" + data.result.items[i].supplier + "</td>"
                            + "<td>" + "<a class=\"btn btn-default btn-sm\" href=${pageContext.request.contextPath}/addDelivery?id=" + data.result.items[i].id +  ">" + "修改" + "</a> "
                            + "<a class=\"btn btn-default btn-sm\" href=\"#\" onclick=\"deleteOutbun('"+ data.result.items[i].id + "')\">删除</a>" + "</td>"
                            + "</tr>";
                        $("#tbody-result").append(strtab);//将数据追加进表格

                        //自定义序号
                        conter++;
                    }

                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }

    function queryOutbunInfo1(){
        var postUrl = "${pageContext.request.contextPath}/member/allInboundOutboundList";
        var str = {
            "userType": "2",
            "productType": $("#user_find").val(),
            "deliveryTime": $("#user_find").val(),
            "supplier": $("#user_find").val()
        };
        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status === 0){
                    $("#user_messge").text(data.msg);
                }else {

                    //列标题，逗号隔开，每一个逗号就是隔开一个单元格
                    let str = "创建ID,商品编号,商品类别,单价,数量,入库时间,出库时间,入库人,出库人,供货商,创建时间,更新时间\n";

                    for (var i = 0; i < data.result.items.length; i++) {
                        for(let item in data.result.items[i]){
                            str += data.result.items[i][item] + "\t" + ",";
                        }
                        str += "\n";
                    }
                    //encodeURIComponent解决中文乱码
                    let uri = "data:text/csv;charset=utf-8,\ufeff" + encodeURIComponent(str);
                    //通过创建a标签实现
                    let link = document.createElement("a");
                    link.href = uri;
                    //对下载的文件命名
                    link.download =  "出库数据表.csv";
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link);

                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }

    //删除方法
    function deleteOutbun(datees){

        //显示提示对话框
        var mModal2 = new mModal({
            top: "40vh",
            content: "你确定要删除此项？",
            confirm: function () {
                deleteOutbun1(datees);
                mModal2.close();
            },
            cancel: function () {
            }
        });

        mModal2.renderDom();
    }


    function deleteOutbun1(id){
        var postUrl = "${pageContext.request.contextPath}/member/deleteInboundOutbound";
        var str = {
            "id": id
        };
        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status === 0){
                    $("#user_messge").text(data.msg);
                }else {
                    location.reload();
                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }

</script>



</body>
</html>
</span>